package com.banana.springcloud.controller;

import com.banana.springcloud.service.OpenFeignOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author layman
 * @date 2021/1/18
 */
@RestController
@RequestMapping("/consumer/hyxtrix")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OpenFeignOrderController {

    @Resource
    private OpenFeignOrderService openFeignOrderService;

    @GetMapping("/success/{id}")
    public String feignSuccess(@PathVariable("id") Long id){
        return openFeignOrderService.feignSuccess(id);
    }

    @GetMapping("/timeout/{id}")
    @HystrixCommand(fallbackMethod = "feignTimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
    public String feignTimeout(@PathVariable("id") Long id){
        return openFeignOrderService.feignTimeout(id);
    }

    public String feignTimeoutHandler(@PathVariable("id") Long id){
        return "对方支付系统繁忙，请稍后重试！";
    }

    /**
    * HystrixCommand注解没有配置属性，走全局fallback方法
    */
    @GetMapping("/global/fallback")
    @HystrixCommand
    public String feignGlobalFallback(){
        String result = null;
        return result.toString();
    }
    /**
     * 全局fallback 降级方法
     */
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后再试，o(╥﹏╥)o";
    }
}
